package com.scs.application.modules.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.base.entity.Patient;
import com.scs.application.modules.base.mapper.PatientMapper;
import com.scs.application.modules.base.service.PatientService;
import com.scs.application.modules.sys.model.RestResult;
import com.scs.application.modules.thirdparty.dto.HisPatient;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 */
@Service
public class PatientServiceImpl extends BaseServiceImpl<PatientMapper, Patient> implements PatientService {

    public List<Patient> patientList(){
        List<Patient> patients = list(Wrappers.<Patient>query());
        return patients;
    }

    public RestResult syncupdata(List<HisPatient> data){
        try {
            List<Patient> patientSaves = new ArrayList<>();
            for (int i = 0;i < data.size();i++){
                HisPatient patients = data.get(i);
                if (StringUtils.isBlank(patients.getPatient_no())){
                    continue;
                }
                List<Patient> patientsList = this.list(new QueryWrapper<Patient>().eq("no", patients.getPatient_no()));
                if (!patientsList.isEmpty()) {
                    continue;
                }

                Patient patient = new Patient();
                patient.setInpatientNo(patients.getInpatient_no())
                        .setInpatientSn(patients.getInpatient_sn())
                        .setOutpatientSn(patients.getOutpatient_sn())
                        .setOutpatientNo(patients.getOutpatient_no())
                        .setOutDeptCode(patients.getOut_dept_code())
                        .setOutDept(patients.getOut_dept())
                        .setInDeptCode(patients.getIn_dept_code())
                        .setInDept(patients.getIn_dept())
                        .setNo(patients.getPatient_no())
                        .setMdate(patients.getMdate())
                        .setType(patients.getPatient_type().toString())
                        .setBedNo(patients.getBed_no())
                        .setRemark(patients.getRemark())
                        .setName(patients.getPatient_name())
                ;
                patientSaves.add(patient);
            }
            this.saveBatch(patientSaves);
        }catch (Exception e){
        }

        return RestResult.ok("同步成功");
    }
}
